/**
 * Created by geyouwen on 16/7/6.
 */

$(document).ready(function(){

    $('.layer-del').click(function(){
        var url = $(this).attr('data-url')
        layer.confirm('确定删除选中项？',function() {
            $.post(url, {}, function (res) {
                if (res.status) {
                    layer.msg(res.msg, {icon: 1, time: 2000}, function () {
                        window.location.reload();
                    });
                } else {
                    layer.msg(res.msg, {icon: 2, time: 2000})
                }
            }, 'json')
        })
    })


   // 弹出网页层
    $('.layer_confirm').click(function(){
        var msg = $(this).attr('data-msg');
        var url = $(this).attr('data-url');
        layer.confirm(msg,{icon:3},function(index){
            $.getJSON(url,function(data){
                if(!data.status)
                {
                    layer.msg(data.msg,{icon:2,time:2000});
                } else {
                    layer.msg(data.msg,{icon:1,time:2000},function(){
                        window.location.reload();
                    });
                }
            })
            layer.close(index);
        });
    })



    //弹出层的绑定事件
    $('.layer-dialog').click(function(){
        //弹出层之前的绑定事件
        var beforefun    = $(this).attr('data-check-before');
        if(typeof(beforefun) != 'undefined') {
            if ($.isFunction(eval('window.'+beforefun))) {
                if (!eval(beforefun+'()')) return false;
            }
        }

        var url    = $(this).attr('data-url');
        var height = $(this).attr('data-height');
        var width  = $(this).attr('data-width');
        var title  = $(this).attr('data-title');
        var btnconf   = $(this).attr('data-btn');
        var btn = new Array();
        if(typeof(btnconf) == 'undefined'){
            btn = ['保存','取消'];
        } else {
            btn = btnconf.split(',');
        }
        layer.open({
            type: 2,
            title: title,
            offset: '110px',
            shadeClose: false,  //点击遮罩以外关闭
            shade: 0.3,  //遮罩
            area: [width, height],
            btn: btn,
            skin: 'layui-layer-rim', //加上边框
            content: url, //iframe的url
            yes:function(index,layero){
                var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                iframeWin.submitEvent();
            }
        });
    });



    //多窗口弹出层的绑定事件
    $('.layer-dialog-more').click(function(){
        //弹出层之前的绑定事件
         var url    = $(this).attr('data-url');
        var height = $(this).attr('data-height');
        var width  = $(this).attr('data-width');
        var title  = $(this).attr('data-title');
        var btnconf   = $(this).attr('data-btn');
        var btn = new Array();
        if(typeof(btnconf) == 'undefined'){
            btn = ['保存','取消'];
        } else {
            btn = btnconf.split(',');
        }
        var it = layer.open({
            type: 2,
            title: title,
            shadeClose: false,  //点击遮罩以外关闭
            shade:0 ,  //遮罩
            area: [width, height],
            btn: false,
            skin: 'layui-layer-rim', //加上边框
            content: url, //iframe的url

            yes:function(index,layero){
               // $(that).click();
            },
            zIndex: layer.zIndex ,//重点1
            maxmin: true,
            success: function(layero){
                layer.setTop(layero); //重点2
            }

        });
        layer.full(it);

    });




    //切换
    $('.J_tabs li').click(function(){
        $(this).siblings('li').removeClass('current');
        $(this).addClass('current');
        var index = $(this).index();
        $('.content_list').hide();
        $('.content_list').eq(index).show();
    });


    //选择分类
    $(document).on('change','.J_cate_select',function(){
        var cate_list = $('#json_cate_list').val();
        var obj = $(this);
        var pid = $(this).val();
        $('#cate_id').val(pid);
        if(cate_list) {
            cate_list = $.parseJSON(cate_list);
        }  else {
            return false;
        }
        if(pid == 'null' || ((typeof(cate_list[pid]) == 'undefined')))
        {
            obj.nextAll('.J_cate_select').remove();
            return false;
        }

        if(typeof(cate_list[pid]) != 'undefined')
        {
            var res = cate_list[pid];
            var str ='';
            var str_start = '<select class="mr10 J_cate_select">';
            var default_str = '<option value="null" data-pid="null">--请选择--</option>'
            $.each(res,function(index,item)
            {
                str += '<option value="'+item.id+'" data-pid="'+item.pid+'">'+item.cate_name+'</option>'
            });
            var str_end  = '</select>'
            var flag = obj.next('.J_cate_select').hasClass('J_cate_select')
            if(str)
            {
                if(!flag) obj.after(str_start + default_str+ str +str_end)
                if(flag)
                {
                    obj.nextAll('.J_cate_select').remove();
                    obj.after(str_start+default_str+str+str_end);
                }
            } else {
                if(flag) obj.nextAll('.J_cate_select').remove();
            }
        }
    });


    //列表图片icon 鼠标划过显示
    $('.attachment_icon').hover(function()
    {
        var src = $(this).attr('file-rel')
        var html = '<div class="attachment_icon_"><img src="'+src+'" width="150" height="150"></div>'
        $(this).append(html);
    }, function(){
        $('.attachment_icon_').remove();
    });



});

var reg_mobile = /^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/;
var reg_email  = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
//正则数据格式验证
function verifyformat($reg,$verifydata)
{
    return ($reg.test($verifydata)) ? true : false
}

//文章分类选择下拉初始化(add)
function cate_select_init()
{
    //分类选择初始化
    var url = $('.J_cate_select').attr('data-uri');
    var pid = $('.J_cate_select').attr('data-pid');

    $.post(url,{pid:pid},function(res){
        if(res)
        {
            var str = '<option value="null" data-pid="null">--请选择--</option>';
            var select = ''
            $.each(res,function(index,item)
            {
                str += '<option value="'+item.id+'" '+select+' data-pid="'+item.pid+'">'+item.cate_name+'</option>'
            });
            $('.J_cate_select').html(str);
        }
    },'json')
}

//初始化分类并选中(edit)
function cate_select_default(cate_source, cate_path)
{
    if(!cate_source || !cate_path)
    {
        cate_select_init();
        return false;
    }
    if(cate_path)
    {
        cate_path = cate_path.split('|');
    }
    if(cate_source) {
        cate_source = $.parseJSON(cate_source);
    }  else {
        return false;
    }
    var option = '<option value="null" data-pid="null">--请选择--</option>';
    $.each(cate_source[0],function(index, item){
        if(cate_path.indexOf(item.id) != -1)
        {
            var select = 'selected = "selected"';
        } else {
            var select = '';
        }
        option += '<option value="'+item.id+'" '+select+'>'+ item.cate_name+'</option>'
    })
    $('.J_cate_select').html(option);

    var parent_path = cate_path.concat();
    parent_path.pop();
    $.each(parent_path,function(index,item){
       var obj =  cate_source[item];
       var str = '<select class="J_cate_select mr10">'
        str += '<option value="null" data-pid="null">--请选择--</option>';
        $.each(obj, function(a,b){
            if(cate_path.indexOf(b.id) != -1)
            {
                var select = 'selected = "selected"';
            } else {
                var select = '';
            }
            str += '<option value="'+ b.id+'" '+select+'>'+ b.cate_name+'</option>'
        })
        str += '</select>'
        $('#cate_id').before(str);
    })



}


function verify(obj, msg)
{
    var value = $(obj).val();
    if($.trim(value)=='')
    {
        layer.tips(msg, obj, {tips:[2, '#3595CC']});
        $(obj).focus();
        return false;
    }
    return true
}

//数据格式验证 正则
function paramsformat(param, type)
{
    var regx = new Array();
    regx['idcard']  = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/
    regx['email']  = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/; //邮箱验证
    regx['mobile'] = /^1[3|4|5|7|8]\d{9}$/; //移动手机验证
    regx['zip']    = /^[0-9]{6}$/; //邮编验证
    if(!regx[type].test(param)) return false;
    return true;
}